package web.controller.sys;

import com.github.pagehelper.PageInfo;
import framework.defined.SystemDefined;
import framework.vo.R;
import framework.vo.ResultInfo;
import framework.vo.ResultList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import system.entity.SysPermission;
import system.service.sys.SysPermissionService;
import web.vo.PermissionOption;

import java.util.List;

@RestController
@RequestMapping("/sys/permission")
@Api(tags = "角色权限")
public class SysPermissionController {
    @Autowired
    private SysPermissionService sysPermissionService;

    @ApiOperation("添加权限表")
    @PostMapping("/add")
    public ResultInfo<String> add(@ModelAttribute SysPermission param) {
        java.util.Date now = new java.util.Date();
        Integer added = sysPermissionService.add(param);
        ResultInfo<String> info = new ResultInfo<>(param.getId());
        return info;
    }

    @ApiOperation("删除权限表")
    @PostMapping("/del")
    public ResultInfo<Integer> del(@RequestParam("ids") List<String> ids) {
        Integer deleted = sysPermissionService.deleteByIds(ids);
        ResultInfo<Integer> info = new ResultInfo<>(deleted);
        return info;
    }

    @ApiOperation("更新权限表")
    @PostMapping("/edit")
    @Transactional
    public ResultInfo<Integer> edit(@ModelAttribute SysPermission param, @RequestParam String oldId) {
        if (!StringUtils.hasText(param.getId()))
            return R.failed("Request param error, not input oldId");
        Integer updated = sysPermissionService.update(param, oldId);
        ResultInfo<Integer> info = new ResultInfo<>(updated);
        return info;
    }

    @ApiOperation("查看权限表")
    @GetMapping("/info")
    public ResultInfo<SysPermission> info(@RequestParam("id") String id) {
        SysPermission info = sysPermissionService.loadById(id);
        return new ResultInfo<>(info);
    }

    @ApiOperation("权限表列表")
    @GetMapping("/list")
    public ResultList<SysPermission> list(SysPermission param, @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<SysPermission> info = sysPermissionService.list(param, pageIndex, pageSize);
        return new ResultList<>(info);
    }

    @ApiOperation("权限表树")
    @GetMapping("/tree")
    public ResultInfo<List<PermissionOption>> tree(@RequestParam(required = false) Boolean onlyEnabled) {
        SysPermission query = new SysPermission();
        if (onlyEnabled == null) {
            //
        } else if (onlyEnabled) {
            query.setIsDisabled(SystemDefined.NO_VALUE);
        } else {
            query.setIsDisabled(SystemDefined.YES_VALUE);
        }
        List<SysPermission> permissions = sysPermissionService.load(query);
        List<PermissionOption> list = PermissionOption.tree(permissions, false);
        return new ResultInfo<List<PermissionOption>>(list);
    }
}